package com.wntms.wgj.service.impl;

import com.github.pagehelper.PageHelper;
import com.wntms.wgj.dao.DriverSettleDao;
import com.wntms.wgj.pojo.DriverSettle;
import com.wntms.wgj.service.DriverSettleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class DriverSettleServiceImpl implements DriverSettleService {

    @Autowired
    private DriverSettleDao driverSettleDao;

    @Override
    public List<DriverSettle> findAll() {
        List<DriverSettle> list = driverSettleDao.findAll();
        for (DriverSettle driverSettle : list) {
            Double money = (driverSettle.getCarriageContracts().getFare()+driverSettle.getCarriageContracts().getInsurance()) * 0.8;
            BigDecimal bigDecimal = new BigDecimal(money);
            double numm = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            driverSettle.setMoney(numm);
        }
        return list;
    }

    @Override
    public List<DriverSettle> findAll(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);

        List<DriverSettle> list = driverSettleDao.findAll();
        for (DriverSettle driverSettle : list) {
            Double money = (driverSettle.getCarriageContracts().getFare()+driverSettle.getCarriageContracts().getInsurance()) * 0.8;
            BigDecimal bigDecimal = new BigDecimal(money);
            double numm = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            driverSettle.setMoney(numm);
        }
        return list;
    }

    @Override
    public boolean updateSettle(DriverSettle driverSettle) {
        return driverSettleDao.updateSettle(driverSettle) == 1 ? true:false;
    }


    @Override
    public boolean deleteSettle(Integer id) {
        return driverSettleDao.deleteSettle(id) == 1 ? true:false;
    }

    @Override
    public DriverSettle findByCid(String cid) {
        DriverSettle driverSettle = driverSettleDao.findByCid(cid);
        Double money = (driverSettle.getCarriageContracts().getFare()+driverSettle.getCarriageContracts().getInsurance()) * 0.8;
        BigDecimal bigDecimal = new BigDecimal(money);
        double numm = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        driverSettle.setMoney(numm);

        return driverSettle;
    }
}
